Flask GET and POST
GET vs POST:
- GET requests can be bookmarked, POST requests cannot
- GET requests remain in browser history, POST requests do not
- GET requests should never be used with sensitive data
- POST is safer than GET because parameters from POST are not stored in browser history or web server logs
templates\template.html
<p>Type something in the text input fields:</p> <form action="/post_input" method="POST"> <input type="text" name="myInput" placeholder="uses POST"> <input type="submit" value="Submit via POST"> </form> <form action="/get_input" method="GET"> <input type="text" name="myInput" placeholder="uses GET"> <input type="submit" value="Submit via GET"> </form>
server.py
from flask import * app = Flask("getting input from the user") @app.route("/") def main(): return render_template("template.html") @app.route("/post_input", methods=["POST"]) def post_input(): message = request.form["myInput"] return render_template_string(message) @app.route("/get_input", methods=["GET"]) def get_input(): message = request.args.get("myInput") return render_template_string(message) app.run(debug=True)